package com.java2401.springbootv2.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2401.springbootv2.pojo.User;
import com.java2401.springbootv2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserHandler {
    @Autowired
    UserService userService;
    @RequestMapping("/select")
    public void selectByPrimaryKey(Integer id) {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxx");
        User user = userService.selectByPrimaryKey(id);
        System.out.println("查询结果---"+user);

    }

    @RequestMapping("/users")
    @ResponseBody
    public PageInfo<User> users(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn,10);
        System.out.println("分页查询控制器");
        List<User> users = userService.allUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(users,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }

    @PostMapping("insert")
    @ResponseBody
    public String insertUser(@RequestBody User user){
        System.out.println("user" + user);
       // String s = insertUser(user);
        String s = userService.insertUser(user);
        return s;
    }

    @DeleteMapping("delete/{id}")
    @ResponseBody
    public Integer deleteUser(@PathVariable("id") Integer id){
        System.out.println("id..........." + id);
        int b = userService.deleteUser(id);
        return b;
    }

    @PutMapping("/update")
    @ResponseBody
    public Integer updateUser(@RequestBody User user){
        Integer i = userService.updateUser(user);
        return i;
    }

    @GetMapping("/selectId")
    @ResponseBody
    public PageInfo<User> selectByUserId(@RequestParam("id") Integer id){
        User user = userService.selectByUserId(id);
        List<User> users = new ArrayList<>();
        users.add(user);
        PageHelper.startPage(1,10);
        PageInfo<User> pageInfo = new PageInfo<>(users,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }


}
